package com.seraph.td.socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;

import com.seraph.td.tools.Handler;

import weibo4andriod.org.json.JSONException;
import weibo4andriod.org.json.JSONObject;

public class UuseeScannerClientTest {
	
	static private  final int DPORT = 25852;
	static private  final int SPORT = 33405;
	static private Thread workThread = new Thread(){

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			DatagramSocket dSocket = null;
			Socket sSocket = null;
			OutputStream out = null;
				try {
					while(true){
					System.out.println("BEGIN------------");
					dSocket = new DatagramSocket(DPORT);
					byte[] buffer = new byte[1024];
					DatagramPacket p = new DatagramPacket(buffer, 1024);
					System.out.println("recive-------0");
					dSocket.receive(p);
					System.out.println(new String(p.getData(),0,p.getLength()));
					System.out.println("recive-------1");
					String str = new String(buffer,0,p.getLength());
					System.out.println(str);
					InetSocketAddress remoteAddr =(InetSocketAddress) p.getSocketAddress();
					String remoteIp = remoteAddr.getAddress().getHostAddress();
					System.out.println(remoteIp);
					sSocket = new Socket();
					System.out.println("connect-------0");
					sSocket.connect(new InetSocketAddress(remoteIp, SPORT));
					System.out.println("connect-------1");
					JSONObject jobj = new JSONObject();
					jobj.put("host", "local_host");
					jobj.put("connect",true);
					out = sSocket.getOutputStream();
					out.write(jobj.toString().getBytes());
					out.write("\n".getBytes());
					out.flush();
					out.close();
					sSocket.close();
					dSocket.close();
					System.out.println("----------------lSocket.receive---------------");
					}
				} catch (SocketException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					if(out !=null )
						try {
							out.close();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					if(dSocket != null && !dSocket.isClosed())
						dSocket.close();
					if(sSocket !=null && !sSocket.isClosed())
						try {
							sSocket.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				}
		
			
			
		}
		
	};
	static class HostInfo{
		String hostName="";
		boolean connected = false;
		
	}
	static public void main(String[] argv){
		Handler handler = new Handler();
//		UuseeScannerClient scannerClient = new UuseeScannerClient(handler);
//		scannerClient.startScanner();
		UuseeScannerClientTest test = new UuseeScannerClientTest();
		test.workThread.start();
//		  In
	  }
}
